<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="名称" prop="honorName">
        <el-input
          v-model="queryParams.honorName"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="简称" prop="alternativeName">
        <el-input
          v-model="queryParams.alternativeName"
          placeholder="请输入简称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="编码" prop="honorCode">
        <el-input
          v-model="queryParams.honorCode"
          placeholder="请输入编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="查询码" prop="lookup">
        <el-input
          v-model="queryParams.lookup"
          placeholder="请输入查询码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="种类" prop="category">
        <!-- <el-input
          v-model="queryParams.category"
          placeholder="请输入种类"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />-->

        <el-select
          v-model="queryParams.category"
          clearable
          size="small"
          placeholder="请选择种类..."
          @change="handleQuery"
        >
          <el-option
            v-for="item in avaiableCategories"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="级别" prop="level">
        <!-- <el-input
          v-model="queryParams.level"
          placeholder="请输入级别"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />-->
        <el-select
          v-model="queryParams.level"
          clearable
          size="small"
          placeholder="请选择级别..."
          @change="handleQuery"
        >
          <el-option
            v-for="item in avaiableLevels"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="label">
        <el-input
          v-model="queryParams.label"
          placeholder="请输入类型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="获奖者" prop="winner">
        <el-input
          v-model="queryParams.winner"
          placeholder="请输入获奖者"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="获奖部门" prop="department">
        <el-input
          v-model="queryParams.department"
          placeholder="请输入获奖部门"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="获奖时间" prop="awardedAt">
        <el-date-picker
          clearable
          size="small"
          v-model="queryParams.awardedAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择获奖时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="颁奖单位" prop="awardedBy">
        <el-input
          v-model="queryParams.awardedBy"
          placeholder="请输入颁奖单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="封面图片" prop="coverId">
        <el-input
          v-model="queryParams.coverId"
          placeholder="请输入封面图片"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="荣誉标签" prop="tags">
        <el-input
          v-model="queryParams.tags"
          placeholder="请输入荣誉标签"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="请选择字典生成" value />
        </el-select>
      </el-form-item>
      <!--       
      <el-form-item label="删除标志" prop="deleted">
        <el-select v-model="queryParams.deleted" placeholder="请选择删除标志" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="显示顺序" prop="displayOrder">
        <el-input
          v-model="queryParams.displayOrder"
          placeholder="请输入显示顺序"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建者" prop="createdBy">
        <el-input
          v-model="queryParams.createdBy"
          placeholder="请输入创建者"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdAt">
        <el-date-picker
          clearable
          size="small"
          v-model="queryParams.createdAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择创建时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="更新者" prop="updatedBy">
        <el-input
          v-model="queryParams.updatedBy"
          placeholder="请输入更新者"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新时间" prop="updatedAt">
        <el-date-picker
          clearable
          size="small"
          v-model="queryParams.updatedAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择更新时间"
        ></el-date-picker>
      </el-form-item>
      -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['archive-honor:honor:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['archive-honor:honor:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['archive-honor:honor:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['archive-honor:honor:export']"
        >导出</el-button>
      </el-col>
      <!-- goCreatePage -->
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="goCreatePage"
          v-hasPermi="['archive-honor:honor:add']"
        >添加</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          :disabled="single"
          size="mini"
          @click="goEditPage"
          v-hasPermi="['archive-honor:honor:edit']"
        >编辑</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="honorList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="荣誉id" align="center" prop="honorId" /> -->
      <el-table-column label="名称" align="left" prop="honorName" width="180" />
      <!-- <el-table-column label="简称" align="center" prop="alternativeName" /> -->
      <el-table-column label="编码" align="center" prop="honorCode" />
      <!-- <el-table-column label="查询码" align="center" prop="lookup" /> -->
      <el-table-column label="种类" align="center" prop="category" :formatter="getDisplayName" />
      <el-table-column label="级别" align="center" prop="level" :formatter="getLevelName" />
      <el-table-column label="类型" align="center" prop="label" />
      <el-table-column label="获奖者" align="center" prop="winner" />
      <el-table-column label="获奖部门" align="center" prop="department" />
      <el-table-column label="获奖时间" align="center" prop="awardedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.awardedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="颁奖单位" align="center" prop="awardedBy" width="150" />
      <!-- <el-table-column label="荣誉描述" align="center" prop="description" /> -->
      <!-- <el-table-column label="封面图片" align="center" prop="coverId" />
      <el-table-column label="荣誉标签" align="center" prop="tags" />
      <el-table-column label="荣誉附件" align="center" prop="attachments" />-->
      <el-table-column label="状态" align="center" prop="status" :formatter="getStatusName" />
      <el-table-column label="删除标志" align="center" prop="deleted">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_show_hide" :value="scope.row.deleted" />
        </template>
      </el-table-column>
      <el-table-column label="显示顺序" align="center" prop="displayOrder" />
      <el-table-column label="录入者" align="center" prop="createdBy" />
      <el-table-column label="录入时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者" align="center" prop="updatedBy" />
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['archive-honor:honor:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['archive-honor:honor:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改荣誉对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="名称" prop="honorName">
                <el-input v-model="form.honorName" placeholder="请输入名称" />
              </el-form-item>
              <el-form-item label="简称" prop="alternativeName">
                <el-input v-model="form.alternativeName" placeholder="请输入简称" />
              </el-form-item>
              <el-form-item label="编码" prop="honorCode">
                <el-input v-model="form.honorCode" placeholder="请输入编码" />
              </el-form-item>
              <el-form-item label="查询码" prop="lookup">
                <el-input v-model="form.lookup" placeholder="请输入查询码" />
              </el-form-item>
              <el-form-item label="种类" prop="category">
                <!-- <el-input v-model="form.category" placeholder="请输入种类" /> -->
                <el-select v-model="form.category" placeholder="请选择种类...">
                  <el-option
                    v-for="item in avaiableCategories"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="级别" prop="level">
                <!-- <el-input v-model="form.level" placeholder="请输入级别" /> -->
                <el-select v-model="form.level" clearable size="small" placeholder="请选择级别...">
                  <el-option
                    v-for="item in avaiableLevels"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="类型" prop="label">
                <el-input v-model="form.label" placeholder="请输入类型" />
              </el-form-item>
              <el-form-item label="获奖者" prop="winner">
                <el-input v-model="form.winner" placeholder="请输入获奖者" />
              </el-form-item>
              <el-form-item label="获奖部门" prop="department">
                <el-input v-model="form.department" placeholder="请输入获奖部门" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="获奖时间" prop="awardedAt">
                <el-date-picker
                  clearable
                  size="small"
                  v-model="form.awardedAt"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择获奖时间"
                ></el-date-picker>
              </el-form-item>
              <el-form-item label="颁奖单位" prop="awardedBy">
                <el-input v-model="form.awardedBy" placeholder="请输入颁奖单位" />
              </el-form-item>
              <el-form-item label="荣誉描述" prop="description">
                <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
              </el-form-item>
              <el-form-item label="封面图片" prop="coverId">
                <el-input v-model="form.coverId" placeholder="请输入封面图片" />
              </el-form-item>
              <el-form-item label="荣誉标签" prop="tags">
                <el-input v-model="form.tags" placeholder="请输入荣誉标签" />
              </el-form-item>
              <el-form-item label="荣誉附件" prop="attachments">
                <el-input v-model="form.attachments" type="textarea" placeholder="请输入内容" />
              </el-form-item>
              <el-form-item label="状态">
                <el-radio-group v-model="form.status">
                  <!-- <el-radio label="1">请选择字典生成</el-radio> -->
                  <el-radio
                    v-for="dict in dict.type.sys_honor_status"
                    :key="dict.value"
                    :label="parseInt(dict.value)"
                  >{{ dict.label }}</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="删除标志">
                <el-radio-group v-model="form.deleted">
                  <el-radio
                    v-for="dict in dict.type.sys_show_hide"
                    :key="dict.value"
                    :label="parseInt(dict.value)"
                  >{{ dict.label }}</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="显示顺序" prop="displayOrder">
                <el-input v-model="form.displayOrder" placeholder="请输入显示顺序" />
              </el-form-item>
            </div>
          </el-col>
        </el-row>

        <!--         
        <el-form-item label="创建者" prop="createdBy">
          <el-input v-model="form.createdBy" placeholder="请输入创建者" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker
            clearable
            size="small"
            v-model="form.createdAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择创建时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="更新者" prop="updatedBy">
          <el-input v-model="form.updatedBy" placeholder="请输入更新者" />
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedAt">
          <el-date-picker
            clearable
            size="small"
            v-model="form.updatedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择更新时间"
          ></el-date-picker>
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHonor, getHonor, delHonor, addHonor, updateHonor, exportHonor } from "@/api/archive-honor/honor";

export default {
  name: "Honor",
  // dicts: ['sys_yes_no'], 
  dicts: ['sys_yes_no', 'sys_show_hide', 'sys_honor_status'],
  data () {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 荣誉表格数据
      honorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      avaiableCategories: [{ id: 1, name: '学校' }, { id: 2, name: '部门' }, { id: 3, name: '个人' }],
      avaiableLevels: [
        { id: 1, name: '学校' },
        { id: 2, name: '市级' },
        { id: 3, name: '省级' },
        { id: 4, name: '国家级' },
        { id: 5, name: '国际级' }],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        honorName: null,
        alternativeName: null,
        honorCode: null,
        lookup: null,
        category: null,
        level: null,
        label: null,
        winner: null,
        department: null,
        awardedAt: null,
        awardedBy: null,
        description: null,
        coverId: null,
        tags: null,
        attachments: null,
        status: null,
        deleted: null,
        displayOrder: null,
        createdBy: null,
        createdAt: null,
        updatedBy: null,
        updatedAt: null
      },
      // 表单参数
      form: {
        category: 1,
        level: 1
      },
      // 表单校验
      rules: {
        honorName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created () {
    this.getList();
  },
  methods: {
    /** 查询荣誉列表 */
    getList () {
      this.loading = true;
      listHonor(this.queryParams).then(response => {
        this.honorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        honorId: null,
        honorName: null,
        alternativeName: null,
        honorCode: null,
        lookup: null,
        category: null,
        level: null,
        label: null,
        winner: null,
        department: null,
        awardedAt: null,
        awardedBy: null,
        description: null,
        coverId: null,
        tags: null,
        attachments: null,
        status: 0,
        deleted: 0,
        displayOrder: null,
        createdBy: null,
        createdAt: null,
        updatedBy: null,
        updatedAt: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getDisplayName (row, column) {
      switch (row.category) {
        case 0:
          return "未确定";
        case 1:
          return "个人";
        case 2:
          return "部门";
        default:
          return "学校";
      }
      // return row.category
    },
    getLevelName (row, column) {
      switch (row.level) {
        case 0:
          return "未确定";
        case 1:
          return "学校";
        case 2:
          return "市级";
        case 3:
          return "省级";
        case 4:
          return "国家级";
        case 5:
          return "世界级";
        default:
          return "学校";
      }
      // return row.category
    },
    getStatusName (row, column) {
      switch (row.status) {
        case 0:
          return "编辑中";
        case 1:
          return "已提交";
        case 2:
          return "确认";
        default:
          return "编辑中";
      }
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.honorId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.form.category = 1
      this.form.level = 1
      this.form.status = 0
      this.open = true;
      this.title = "添加荣誉";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const honorId = row.honorId || this.ids
      getHonor(honorId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改荣誉";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.honorId != null) {
            updateHonor(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHonor(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const honorIds = row.honorId || this.ids;
      this.$modal.confirm('是否确认删除荣誉编号为"' + honorIds + '"的数据项？').then(function () {
        return delHonor(honorIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有荣誉数据项？').then(() => {
        this.exportLoading = true;
        return exportHonor(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => { });
    },
    goCreatePage () {
      this.$router.push('/honor/honor/add')
    },
    goEditPage (row) {
      if (!row.honorId && !this.ids) {
        this.$message.error("请先选择一个荣誉");
        return
      }
      const honorId = row.honorId || this.ids
      // console.log(honorId, this.ids)
      // return
      this.$router.push('/honor/honor/edit/' + honorId)
    },

  }
};
</script>
